/**
 * 
 */
package cn.edu.ujn.common.elasticSearch;

import java.lang.reflect.Constructor;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsModule;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 * @author 曹仁道
 * @方法描述：创建一个TransportClient 实例
 */
public class CreateTransportClient {
	private static String host="127.0.0.1";
	private static String port="9200";
	private static String clusterName="testSearch";
	static Map<String, String> m = new HashMap<String, String>();
    // 设置client.transport.sniff为true来使客户端去嗅探整个集群的状态，把集群中其它机器的ip地址加到客户端中，
    static Settings settings = Settings.settingsBuilder().put(m).put("cluster.name",clusterName).put("client.transport.sniff", true).build();
 
    // 创建私有对象
    private static TransportClient client;
 
    static {
        try {
            Class<?> clazz = Class.forName(TransportClient.class.getName());
            Constructor<?> constructor = clazz.getDeclaredConstructor(Settings.class);
            constructor.setAccessible(true);
            client = (TransportClient) constructor.newInstance(settings);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
 
    // 取得实例
    public static synchronized TransportClient getTransportClient() {
        return client;
    }
}
